package com.zhuiyi.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyi.zhxy.pojo.Grade;
import com.zhuiyi.zhxy.service.GradeService;
import com.zhuiyi.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2023/1/22 21:05
 **/
@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {
    //sms/gradeController/getGrades/1/3?gradeName=s
    @Autowired
    private GradeService gradeService;

    //sms/gradeController/getGrades
    @ApiOperation("获取所有Grade")
    @GetMapping("/getGrades")
    public Result getGrades(){
        List<Grade> grades = gradeService.getGrades();
        return Result.ok(grades);
    }

    ///sms/gradeController/deleteGrade
    @ApiOperation("删除Grade，传JSON 的要删除的id数组")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(@ApiParam("要删除的id数组") @RequestBody List<Integer> ids){
        boolean result = gradeService.removeByIds(ids);
        if (result) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("新增或修改Grade,有id属性就是修改，没有就是新增")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@RequestBody Grade grade){
        //接受参数
        //调用service
        boolean result = gradeService.saveOrUpdate(grade);
        if (result) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("分页带条件查询Grade")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(
            @ApiParam("当前多少页") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("每页多少条记录") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("分页查询条件") @RequestParam(value = "gradeName",required = false) String gradeName
    ){
        //封装page对象
        Page<Grade> page = new Page<>(pageNo,pageSize);
        //调用gradeService返回分页对象
        IPage<Grade> gradeIPage = gradeService.getGradesByPage(page,gradeName);
        //将分页对象封装到Result返回
        return Result.ok(gradeIPage);
    }
}
